package com.bwie.servicemodule.base;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bwie.servicemodule.controler.OkGoControler;

import io.reactivex.Observable;

/**
 * fragment界面封装
 * */
public abstract class BaseFragment extends Fragment implements OkGoControler.HttpListener {

    private View view;
    protected OkGoControler okGoControler;
    protected final String TAG = "ghd";
    protected String uri = "http://api.yunzhancn.cn/api/app.interface.php?siteid=78703&";
    protected SharedPreferences preferences;
    protected SharedPreferences.Editor editor;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate( setView(), container, false );

        preferences = getActivity().getSharedPreferences( "jiancai", getActivity().MODE_PRIVATE );
        editor = preferences.edit();

        init();
        initFragmentView();
        return view;
    }



    private void init() {
        okGoControler = new OkGoControler();
        okGoControler.setListener( this );
    }

    /**
     * msg: 获取fragment的视图
     */
    protected abstract int setView( );

    /**
     * msg : 初始化控件的方法
     */
    protected abstract void initFragmentView();


    /**
     * msg:查找控件的方法
     */
    public <T extends View>T f( int id) {
        T t = view.findViewById( id );
        return t;
    }

    /**
     * msg:界面跳转方法
     * */
    public void startUI(Class clazz){
        Intent intent = new Intent(getActivity(),clazz);
        startActivity(intent);
    }
    /**
     * msg:界面跳转方法
     * */
    public void startUI_Finish(Class clazz){
        Intent intent = new Intent(getActivity(),clazz);
        startActivity(intent);
    }
    /**
     * msg:界面跳转方法
     * */
    public void startUI(Intent intent){
        startActivity(intent);
    }
    /**
     * msg:界面跳转方法
     * */
    public void startUI_Finish(Intent intent) {
        startActivity( intent );
    }


    @Override
    public void onSuccess(String url, String result) {
        Log.e( TAG, "onSuccess: " + result  );
    }
    @Override
    public void onSuccess(String url, String result, int type) {
        Log.e( TAG, "onSuccess: " + result  );
    }
    @Override
    public void onErro(String url, String msg) {

    }
}
